package com.his.struts.action;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.foods.FoodsDao;
import com.his.struts.dao.foods.FoodsImp;
import com.his.struts.form.FoodtypeForm;
import com.his.struts.util.AjaxUtil;
import com.his.struts.util.TranCharset;

/**
 * 食物类别类
 * 
 * @author master
 * 
 */
public class FoodtypeAction extends DispatchAction {
	FoodsDao foodsDao;

	/**
	 * 食物类型联动
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getTypes(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ArrayList<FoodtypeForm> typelist = new ArrayList<FoodtypeForm>();
		foodsDao = new FoodsImp();
		String stype = request.getParameter("type1");
		typelist = foodsDao.getTypelist(stype);
		AjaxUtil.send(response, typelist);
		return null;
	}

	/**
	 * 添加食物类型
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addType(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PrintWriter out = response.getWriter();
		FoodtypeForm foodtypeForm = (FoodtypeForm) form;
		String ty = foodtypeForm.getTpcname();
		ty = TranCharset.TranEncodeTOUTF_8(ty);
		foodtypeForm.setTpcname(ty);
		foodsDao = new FoodsImp();
		foodsDao.addType(foodtypeForm);
		out.print("b");
		ActionForward forward = mapping.findForward("addtype");
		request.setAttribute("message", "添加成功");
		return forward;
	}
}